package com.rover.system.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.grid.ViewField;

/**
 * 角色.
 *
 */
@Entity
@Table(name = "S_Roles")
public class Role  implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@ViewField
	@Id
	@GeneratedValue(generator = "paymentableGenerator")
    @GenericGenerator(name = "paymentableGenerator", strategy = "native",
            parameters = { @org.hibernate.annotations.Parameter(name = "sequence", value = "seq_S_Role") })
	private Integer id;
	
	@ViewField(header = "角色名称", width = 200)
	@Column(nullable = false, unique = true)
	private String name;
	
	@ViewField(header = "描述", width = 300)
	private String description;
	
	@ViewField
	@Transient
	private String resources;

    @ManyToMany(fetch = FetchType.LAZY)//用LAZY问题：Role不能直接使用role.permissions 会报：com.sun.jdi.InvocationException occurred invoking method.
    @JoinTable(name = "role_permission", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {@JoinColumn(name = "permission_id")})
    @OrderBy("permissionId")
    private Set<Permission> permissions = new HashSet<Permission>();
    
    public void addPermission(Permission permission){
        permissions.add(permission);
    }
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setResources(String resources) {
		this.resources = resources;
	}
	
	public String getResources() {
		return resources;
	}
    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }
    
    
    
}
